#include "buffclass.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

BuffClass::BuffClass(long long bufflen)
        : m_sumbufflen(bufflen)
{
    m_readedbufflen = 0;
    m_usedbufflen = 0;
    m_buffsrc = new char[bufflen];
    assert(m_buffsrc != NULL);
    p1 = (pp)(m_buffsrc);
}

BuffClass::~BuffClass()
{
    delete[] m_buffsrc;
}

char * BuffClass::GetLine()
{
    long long startx = m_usedbufflen;
    for(long long index = m_usedbufflen; index < m_readedbufflen; ++index)
    {
        if(m_buffsrc[index] == '\r')
        {
            m_buffsrc[index] = 0;
        }
        if(m_buffsrc[index] == '\n') // readline
        {
            m_buffsrc[index] = 0;
            m_usedbufflen = index + 1;
            return m_buffsrc + startx;
        }
        
    }
    
    return NULL;
}

bool BuffClass::Empty() const
{
    if(m_readedbufflen == 0)
    {
        return true;
    }
    return false;
}

void BuffClass::ClearUsedbuff()   // clean used buff
{
    assert( m_readedbufflen >= m_usedbufflen);
    if(m_usedbufflen > 0)
    {
        memcpy(m_buffsrc, m_buffsrc + m_usedbufflen,
                m_readedbufflen - m_usedbufflen);
        m_readedbufflen -= m_usedbufflen;
        m_usedbufflen = 0;
    }
}

char * BuffClass::GetBuff()
{
    return m_buffsrc + m_readedbufflen;
}

const long long BuffClass::CanAddBufflen()
{
    return m_sumbufflen - m_readedbufflen;
    
}

void BuffClass::AddUsedData(const long long m)
{
    m_readedbufflen += m;
}

